﻿using System; 
using System.Linq; 
using System.IO; 
using System.IO.IsolatedStorage; 
using System.Collections.Generic; 
using Microsoft.LightSwitch; 
using Microsoft.LightSwitch.Framework.Client; 
using System.Windows.Controls; 
using Microsoft.LightSwitch.Presentation; 
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication.UserCode 
{ 
    public class DialogHelper 
    { 
        Microsoft.LightSwitch.Client.IScreenObject _screen; 
        Microsoft.LightSwitch.Client.IVisualCollection _collection; 
        String _dialogName;

        bool _isEditing; 
        bool _isOK;  //added to allow for user clicking on X 
        bool _doOnce; //added to allow only one closing handler

        double width = -1;
        double height = -1;
        IEntityObject _entity;

        public DialogHelper(Microsoft.LightSwitch.Client.IVisualCollection visualCollection, 
                     String dialogName) 
        { 
            _screen = visualCollection.Screen; 
            _collection = visualCollection; 
            _dialogName = dialogName; 
            _isOK = false; //added to allow for user clicking on X 
            _doOnce = true; //added to allow only one closing handler 
        }

        public DialogHelper(Microsoft.LightSwitch.Client.IVisualCollection visualCollection,
                     String dialogName, double w, double h)
        {
            width = w;
            height = h;

            _screen = visualCollection.Screen;
            _collection = visualCollection;
            _dialogName = dialogName;
            _isOK = false; //added to allow for user clicking on X 
            _doOnce = true; //added to allow only one closing handler 
        }

        public void InitializeUI() 
        { 
            // following moved to BaseOpenDialog as workaround for beta2 
            //bug: Modal window does not fire control available on open. 
            _screen.FindControl(_dialogName).ControlAvailable += new EventHandler<ControlAvailableEventArgs>(DialogHelper_ControlAvailable);
            _screen.FindControl(_dialogName).ControlUnavailable += new EventHandler<ControlUnavailableEventArgs>(DialogHelper_ControlUnavailable); 
        }

        void DialogHelper_ControlAvailable(object sender, ControlAvailableEventArgs e) 
        { 
            if (_doOnce) 
            { 
                ChildWindow childwindow = e.Control as ChildWindow;

                if (width != -1)
                {
                    childwindow.Width = width;
                }

                if (height != -1)
                {
                    childwindow.Width = width;
                }

                childwindow.Closed += new EventHandler(childwindow_Closed);
                
                _doOnce = false; 
            } 
            _screen.FindControl(_dialogName).ControlUnavailable -= new 
                    EventHandler<ControlUnavailableEventArgs>(DialogHelper_ControlUnavailable); 
        }

        void childwindow_Closed(object sender, EventArgs e) 
        { 
            if (_entity != null) 
            { 
                if (_isOK || IsAllwayUpdate) 
                { 
                    (_entity.Details as System.ComponentModel.IEditableObject).EndEdit(); 
                } 
                else 
                { 
                    if (_isEditing) 
                    { 
                        //cancel edit on close without ok (cancel or click X) 
                        (_entity.Details as System.ComponentModel.IEditableObject).CancelEdit(); 
                    } 
                    else 
                    { 
                        //on add if not ok discard, do not add blank row 
                        _entity.Details.DiscardChanges(); 
                    } 
                } 
                _isOK = false; 
            } 
        }

        public bool CanEditSelected() 
        { 
            return _collection.CanEdit && (_collection.SelectedItem != null); 
        }

        public bool CanAdd() 
        { 
            return _collection.CanAddNew; 
        }

        public void AddEntity(string title) 
        { 
            _isEditing = false; 
            _collection.AddNew();
            _screen.FindControl(_dialogName).DisplayName = title; 
            BaseOpenDialog(); 
        }

        public void AddEntity()
        {
            AddEntity("Thêm  " +
                 _collection.Details.GetModel().ElementType.Name);
        }

        public void EditSelectedEntity() 
        { 
            EditSelectedEntity("Chỉnh sửa " + 
                _collection.Details.GetModel().ElementType.Name);            
        }

        public void EditSelectedEntity(string title)
        {
            _isEditing = true;
            _screen.FindControl(_dialogName).DisplayName = title;
            BaseOpenDialog();
        }

        private void BaseOpenDialog() 
        { 
            _entity = _collection.SelectedItem as IEntityObject; 
            if (_entity != null) 
            { 
                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() => 
                { 
                    (_entity.Details as System.ComponentModel.IEditableObject).EndEdit(); 
                    (_entity.Details as System.ComponentModel.IEditableObject).BeginEdit(); 
                } 
                    ); 
                _screen.OpenModalWindow(_dialogName); 
            } 
        }

        void DialogHelper_ControlUnavailable(object sender, ControlUnavailableEventArgs e) 
        { 
            _screen.FindControl(_dialogName).ControlAvailable -= new 
                    EventHandler<ControlAvailableEventArgs>(DialogHelper_ControlAvailable); 
        }

        public void DialogOK() 
        { 
            _isOK = true; 
            _screen.CloseModalWindow(_dialogName); 
        }

        public void DialogCancel() 
        { 
            _screen.CloseModalWindow(_dialogName); 
        }

        public bool IsAllwayUpdate { get; set; }
 
    } 
}